import { createSlice } from '@reduxjs/toolkit';


export const koalaFilePanelSlice = createSlice({
    name: 'koalaFilePanelSlice',
    initialState: {
        fileHistory: [],
        fileParentId: 0,
        menus: false
    },
    reducers: {
        changeFileParentId: (state, action) => {
            state.fileHistory.push(state.fileParentId);
            state.fileParentId = action.payload
        },
        backward: (state) => {
            if (state.fileHistory.length > 0) {
                let p = state.fileHistory.pop()
                state.fileParentId = p;
            }
        },
        backwardUnit: (state, action) => {
            if (action.payload === state.fileParentId) {
                return
            }
            if (action.payload === 0) {
                state.fileHistory = []
            } else {
                while (state.fileHistory.length > 0 && state.fileHistory[state.fileHistory.length-1] !== action.payload) {
                    state.fileHistory.pop()
                }
                if (state.fileHistory.length > 0 && state.fileHistory[0] === action.payload) {
                    state.fileHistory.pop()
                }
            }
            
            state.fileParentId = action.payload
        },
        openMenu: (state) => {
            state.menus = true;
        },
        closeMenu: (state) => {
            state.menus = false;
        },
        setAppBarMenuState: (state, action) => {
            state.menus = action.payload;
        },
    }
});

export const { changeFileParentId, backward, backwardUnit, closeMenu, openMenu, setAppBarMenuState } = koalaFilePanelSlice.actions

export default koalaFilePanelSlice.reducer 